// shouye/findcompany/findcompany.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    companys: [ ],
    username: '机构名称',
    pageNo:2,
    name:'德智体美教育机构',
    qingkuang: '德智体美全面发展',
    subjects: '小学数学，小学语文，奥数。初中数学，初中物理，初中化学',
    address: '成都市春熙路'
  },
  search:function(event){
    var that=this
 
     var name=event.detail.value
    wx.showToast({
      title: '搜索中请稍后...',
      icon: 'loading',
      duration: 2000
    })
     wx.request({
       url: app.globalData.baseUrl+'mechanism/name',
       data:{
         name:name,
         pageNo:1,
         pageSize:30
       },
       method:'get',
       header:{
         'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
       },
       success:function(res){
       
         try{
           that.setData({
             companys: []
           })
         

           var resData = res.data.data.data
           if(resData.length==0){
             wx.showToast({
               title: '暂无结果',
               icon:'none',
               duration:4000
             })
           }
           for (var i = 0; i < resData.length; i++) {
             var img = 'companys[' + i + '].imgurl'
             var name = 'companys[' + i + '].name'
             var zhongzhi = 'companys[' + i + '].zhongzhi'
             var subjects = 'companys[' + i + '].subjects'
             var id = 'companys[' + i + '].id'
             var address = 'companys[' + i + '].address'
             var pur = resData[i].purpose
             if(pur.length>13){
                pur=pur.substring(0,13)+'...'
             }
             var resImg=resData[i].img
             console.log('resImg')
             console.log(resImg)
              if(resImg==null){
            
                resImg ='http://icon.tongchengyouzhi.com/image//2.jpg'
              }
             var sub = resData[i].businessScope
             console.log(sub.length)
             if (sub.length > 13) {
               sub = sub.substring(0, 13) + '...'
             }
             var add = resData[i].position
             if (add.length > 13) {
               add = add.substring(0, 13) + '...'
             }
             that.setData({
               [img]: resImg,
               [name]: resData[i].name,
               [zhongzhi]:pur,
               [subjects]:sub,
               [id]: resData[i].id,
               [address]: add

             })
           }
         }catch(err){
           console.log(err)
          //  wx.showToast({
          //    title: '暂无搜索结果',
          //    icon: 'none'
          //  })
         }
       
       }
     })
  },
  toXiangqing:function(event)
  {
    var id=event.currentTarget.dataset.id

     wx.navigateTo({
       url: '../xiangqing/xiangqing?id='+id,
     })
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getMoreList:function(pageNo){
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'mechanism/list',
      method: 'GET',
      data:{
        pageNo:pageNo,
        pageSize:10
      },  
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        var resData = res.data.data.data
        for (var i = (pageNo-1)*10; i <((pageNo-1)*10)+ res.data.data.data.length; i++) {
          var img = 'companys[' + i + '].imgsrc'
          var name = 'companys[' + i + '].name'
          var zhongzhi = 'companys[' + i + '].zhongzhi'
          var subjects = 'companys[' + i + '].subjects'
          var id = 'companys[' + i + '].id'
          var address = 'companys[' + i + '].address'

          try {
            var resImg = resData[i%10].img
            console.log('resImg')
            console.log(resImg)
            if (resImg == null) {

              resImg = 'http://icon.tongchengyouzhi.com/image//2.jpg'
            }
            var name1 = resData[i%10].name
            if (name1.length > 10) {
              name1 = name1.substring(0, 8) + '...'
            }
            var pur = resData[i%10].purpose
            console.log(pur.length)
            if (pur.length > 12) {
              pur = pur.substring(0, 10) + '...'
            }
            var sub = resData[i%10].businessScope
            if (sub != null) {
              console.log(sub.length)
              if (sub.length > 13) {
                sub = sub.substring(0, 13) + '...'
              }
            }

            var add = resData[i%10].position

            console.log(add.length)
            if (add.length > 12) {
              add = add.substring(0, 10) + '...'
            }
          } catch (err) {
            console.log(err)
            var sub = resData[i%10].businessScope
          }
          try{
            that.setData({
              [img]: resImg,
              [name]: name1,
              [zhongzhi]: pur,
              [subjects]: sub,
              [id]: resData[i % 10].id,
              [address]: add
            })
          }catch(err){
            console.log(err)
          }
          
        }
      }
    })
  },
  onLoad: function (options) {
    this.getMoreList(1)
  },
  callBackFun:function(res){
    

    var data=res.data.data
    var id=data[0].id
 
 
    this.setData({
      'companys[0].id':id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMoreList(this.data.pageNo)
    this.setData({
      pageNo:this.data.pageNo+1
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})